package com.sangu.dianxiu.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.BaseApp;
import com.sangu.dianxiu.ICallBack;
import com.sangu.dianxiu.R;
import com.sangu.dianxiu.utils.RepairOrderManager;

import java.util.HashMap;

public class CancelOrderActivity extends BaseActivity {

    private RepairOrderManager repairOrderManager=new RepairOrderManager();         //使用此类进行网络请求
    private int orderId;                //订单id
    private int check1Count;            //通过点击选择框的次数来显示选择框的图片，此为点击选择框1的次数
    private int check2Count;            //通过点击选择框的次数来显示选择框的图片，此为点击选择框2的次数
    private int check3Count;            //通过点击选择框的次数来显示选择框的图片，此为点击选择框3的次数
    private int check4Count;            //通过点击选择框的次数来显示选择框的图片，此为点击选择框4的次数
    @ViewInject(R.id.iv_activity_cancel_back)
    private ImageView ivBack;           //actionbar上的返回的图标
    @ViewInject(R.id.tv_activity_cancel_order_reason1)              //使用xutils的注解，这样就不用findviewbyid了
    private TextView tvReason1;
    @ViewInject(R.id.tv_activity_cancel_order_reason2)
    private TextView tvReason2;
    @ViewInject(R.id.tv_activity_cancel_order_reason3)
    private TextView tvReason3;
    @ViewInject(R.id.tv_activity_cancel_order_reason4)
    private TextView tvReason4;
    @ViewInject(R.id.iv_activity_cancel_order_reason1)
    private ImageView ivReason1;
    @ViewInject(R.id.iv_activity_cancel_order_reason2)
    private ImageView ivReason2;
    @ViewInject(R.id.iv_activity_cancel_order_reason3)
    private ImageView ivReason3;
    @ViewInject(R.id.iv_activity_cancel_order_reason4)
    private ImageView ivReason4;
    @ViewInject(R.id.et_activity_cancel_order_other)
    private EditText etOtherReason;
    @ViewInject(R.id.btn_activity_cancel_order_submit)
    private Button btnSubmit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cancel_order);
        ViewUtils.inject(this);                                     //只有加上这一句上面的注解才会生效
        orderId=getIntent().getIntExtra("orderId",0);               //获得从上一个页面传过来的订单的ID
    }
    //使用xutils的点击事件注解方式，这样就不用setonclicklistener了
    @OnClick({R.id.iv_activity_cancel_back,R.id.iv_activity_cancel_order_reason1,R.id.iv_activity_cancel_order_reason2,R.id.iv_activity_cancel_order_reason3
    ,R.id.iv_activity_cancel_order_reason4,R.id.btn_activity_cancel_order_submit})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.iv_activity_cancel_back:
                finish();                               //如果点击了返回图标，则结束此activity
                break;
            case R.id.iv_activity_cancel_order_reason1:
                check1Count++;                          //每点击一次此图片，就要计数一次
                if(check1Count%2==1)                        //如果点击的次数模2余1，则应该设置图片为选中图片
                {
                    ivReason1.setImageResource(R.mipmap.order_checked2x);
                }else{
                    ivReason1.setImageResource(R.mipmap.order_uncheck2x);
                }
                break;
            case R.id.iv_activity_cancel_order_reason2:
                check2Count++;
                if(check2Count%2==1)
                {
                    ivReason2.setImageResource(R.mipmap.order_checked2x);
                }else{
                    ivReason2.setImageResource(R.mipmap.order_uncheck2x);
                }
                break;
            case R.id.iv_activity_cancel_order_reason3:
                check3Count++;
                if(check3Count%2==1)
                {
                    ivReason3.setImageResource(R.mipmap.order_checked2x);
                }else{
                    ivReason3.setImageResource(R.mipmap.order_uncheck2x);
                }
                break;
            case R.id.iv_activity_cancel_order_reason4:
                check4Count++;
                if(check4Count%2==1)
                {
                    ivReason4.setImageResource(R.mipmap.order_checked2x);
                }else{
                    ivReason4.setImageResource(R.mipmap.order_uncheck2x);
                }
                break;
            case R.id.btn_activity_cancel_order_submit:
                HashMap<String,String> map=new HashMap<>();
                map.put("userId", BaseApp.user.getUserId()+"");
                map.put("orderId",orderId+"");
                //map.put("phrases",);
                map.put("reason",etOtherReason.getText().toString());
                repairOrderManager.cancelAfterAccept(map, new ICallBack() {
                    @Override
                    public void onSuccess(String string) {
                        Toast.makeText(CancelOrderActivity.this,"取消订单成功",Toast.LENGTH_SHORT).show();        //如果取消订单成功则提示用户
                    }

                    @Override
                    public void onError(VolleyError error) {

                    }
                });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在退出activity的时候取消网络请求，这样可以节省流量，也正是使用volley框架的优点之一
        repairOrderManager.requestQueue.cancelAll(RepairOrderManager.class);
    }
}
